<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>

<script>
import FileUpload from "@/components/FileUpload/index";
export default {
  mixins: [
          Vue.prototype.$vFN.Mixins.Base,
          Vue.prototype.$vFN.Utils,
          ],
  components: { FileUpload },
  data() {
    return {
      load: true,

      form: {
        
        isOpen: "0",                    //  是否开启汇聚支付
        isOpenMa: "0",                 //  是否开启小程序支付
        isOpenMp: "0",                 //  是否开启公众号支付
        isOpenWeb: "0",               //  是否开启H5支付
        maAppId: "",                 // 小程序APPID
        maOriginalId: "",            // 小程序原始ID
        merchantKey: "",             // 商户秘钥
        merchantNo: "",             // 商户编号
        mpAppId: "",                // 公众号APPID
        mpOriginalId: "",          // 公众号原始ID
        tradeMerchantNo: "",       // 报备商户号

      },
      
      show: true,
      
      apiUrl: {
        detail: "mall-site-portal/huiju-pay-config/getItem",
        save: "mall-site-portal/huiju-pay-config/saveOrUpdate",
      },
    };
  },
  mounted() {},
  methods: {
    init() {
      this.getData();
    },

    
    // 保存
    async submit() {

    if(!this.verification()){
        return false;
      }

      this.load = true;
     
      let Parameter = this.getParameter();
    
      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.save,
        data: Parameter,
      },window);

      if (_data._ok) {
        this.$vFN.Tools.messageSuccess("保存修改成功");
        this.init();
      }

       this.load = false;
    },

    // 验证
    verification(){

       // 如果开启汇聚支付,对应的内容一定要填。
      if(this.form.isOpen == 1){
        if(this.$vFN.Utils.isEmpty(this.form.merchantKey) || this.$vFN.Utils.isEmpty(this.form.merchantNo) || this.$vFN.Utils.isEmpty(this.form.tradeMerchantNo)){
            this.$vFN.Tools.messageError("请添写商户秘钥、商户编号、报备商户号")
          return false;
        }
      }

      // 如果开启了小程序或者公众号,,对应的内容一定要填。
      if(this.form.isOpenMa == 1){
        if(this.$vFN.Utils.isEmpty(this.form.maAppId) || this.$vFN.Utils.isEmpty(this.form.maOriginalId)){
           this.$vFN.Tools.messageError("请添写小程序APPID和小程序原始ID")
          return false;
        }
      }
      if(this.form.isOpenMp == 1){
        if(this.$vFN.Utils.isEmpty(this.form.mpAppId) || this.$vFN.Utils.isEmpty(this.form.mpOriginalId)){
           this.$vFN.Tools.messageError("请添写公众号APPID和公众号原始ID")
          return false;
        }
      }

      return true;
    },

    // 搜集参数
    getParameter(){
      let obj = {
          isOpen: this.form.isOpen,
          isOpenWeb: this.form.isOpenWeb,
          isOpenMa:  this.form.isOpenMa,
          isOpenMp: this.form.isOpenMp,
          merchantKey: this.form.merchantKey,
          merchantNo: this.form.merchantNo,
          tradeMerchantNo: this.form.tradeMerchantNo,
      }

     // 需要判断是否，是开启小程序或者公众号
     if(!this.$vFN.Utils.isEmpty(this.form.isOpenMa) && this.form.isOpenMa == 1){
         obj['maAppId'] = this.form.maAppId;
         obj['maOriginalId'] = this.form.maOriginalId;
     }
     if(!this.$vFN.Utils.isEmpty(this.form.isOpenMp) && this.form.isOpenMp == 1){
         obj['mpAppId'] = this.form.mpAppId;
         obj['mpOriginalId'] = this.form.mpOriginalId;
     }


    return obj;
    },

    // 获取内容信息
    async getData() {
      this.load = true;

      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.detail,
      },window);
      if (_data._ok) {
       
           for(let i in this.form){
            if(!this.$vFN.Utils.isEmpty(_data.data[i])){
                this.form[i] = _data.data[i].toString();
             }
           }

          // 如果添加过密钥，需要显示加密
          if(!this.$vFN.Utils.isEmpty(_data.data.merchantKeyDesensitized)){
               this.form.merchantKey = _data.data.merchantKeyDesensitized;
          }
         
      }

     this.load = false;
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>
